﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace class10Homework
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            trysearch();
        }
        public  void trysearch()
        {
            websearch web = new websearch(label2,label3,textBox1);
            web.getResultFromBaidu();
            web.getResultFrombiying();
        }
        

    }
    
    public class websearch
    {
        protected Control Label1;
        protected Control TextBox1;
        protected Control Label2;

        public websearch(Control Label1,Control Label2, Control TextBox1)
        {
            this.Label1 = Label1;
            this.TextBox1 = TextBox1;
            this.Label2 = Label2;

        }
        string GetChinese(String content)  //处理字符串
        {
            content = System.Text.RegularExpressions.Regex.Replace(content, @"[^(\u4e00-\u9fa5)]", "");
            content = System.Text.RegularExpressions.Regex.Replace(content, @"[(\( + \))*]", "");
            if (content.Length >= 200)
                content = content.Substring(0, 200);
            return content;
        }
        async Task<string> WebAsync(string url)
        {
            HttpClient client = new HttpClient();
            Task<string> task = client.GetStringAsync(url); 
            string urlContents = await task; 
            return urlContents;
        }
        async public void getResultFromBaidu()
        {
            string text = TextBox1.Text;
            String text2 = text.Trim();
            String urltext, urltext2;
            String urlOfBaidu = "http://www.baidu.com/baidu?wd=" + text2;
            urltext = await WebAsync(urlOfBaidu);
            urltext = GetChinese(urltext);
            Label1.Text = urltext;
        }
        async public void getResultFrombiying()
        {
            string text = TextBox1.Text;
            String text2 = text.Trim();
            String urltext, urltext2;
            String urlOfbiying = "https://cn.bing.com/search?form=MOZTSB&pc=MOZI&q=" + text2;
            urltext = await WebAsync(urlOfbiying);
            urltext = GetChinese(urltext);
            Label2.Text = urltext;
        }
    }
   
}
